package com.css.dzj.dzjap.modules.info.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.info.entity.ApExpertEntity;
import com.css.dzj.dzjap.modules.info.service.ExpertService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 专家信息
 * @author liangxingguo
 */
@RestController
@RequestMapping("/info")
public class ExpertController {
    @Autowired
    ExpertService expertService;
    @Autowired
    BaseService baseService;
  @GetMapping("/listexpert")
    public R getPageExpert(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                          @RequestParam(name = "expertName", required = false) String expertName,
                          @RequestParam(name = "expertIdcard", required = false) String expertIdcard,
                           @RequestParam(name = "endTime", required = false) String endTime,
                           @RequestParam(name = "startTime", required = false) String startTime,
                          @RequestParam(name = "expertLevl", required = false) String expertLevl,
                            @RequestParam(name = "isBlackList", required = false) String isBlackList,
                           @RequestParam(name="expertTel", required=false) String expertTel){
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults page=expertService.getPage(pageRequest,expertName,expertIdcard,endTime,startTime,expertLevl,isBlackList, expertTel);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("page",page);
    }

    /***
     * 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/delConstr/{id}")
    public R delExpert(@PathVariable String id){
        boolean flag= expertService.delExpert(id);
        if (flag){
            baseService.logSuccess(3,"删除成功","2");
            return R.ok("删除成功");
        }
        baseService.logFailed(3,"删除失败","2");
        return R.error("删除失败");
    }

    /***
     * 更新专家信息
     * @param apExpertEntity
     * @return
     */
    @PostMapping("/update")
    public R updateConstr(@RequestBody ApExpertEntity apExpertEntity){
        try{
            ApExpertEntity apExpertEntity1=expertService.findById(apExpertEntity.getExpertId());
            BeanUtil.copyBeanNotEmptyToBean(apExpertEntity,apExpertEntity1);
            apExpertEntity.setUpdateTime(new Date());
            expertService.update(apExpertEntity1);
            baseService.logSuccess(3,"更新成功","2");
            return R.ok();
        }catch (Exception e){
            baseService.logFailed(3,"更新失败","2");
            return R.error(e.getMessage());
        }
    }

    /***
     * 根据ID查询专家
     * @param id
     * @return
     */
    @PostMapping("/findbyid/{id}")
    public R findById(@PathVariable String id){
        ApExpertEntity apExpertEntity=expertService.findById(id);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("apExpertEntity",apExpertEntity);
    }

    /***
     * 添加专家
     * @param apExpertEntity
     * @return
     */
    @PostMapping("/save")
    public R saveExpert(@RequestBody ApExpertEntity apExpertEntity){
        apExpertEntity.setExpertCreatTime(new Date());
        apExpertEntity.setExpertDelFlag("0");
        if(StringUtils.isEmpty(apExpertEntity.getExpertName())|| StringUtils.isEmpty(apExpertEntity.getExpertIdcard())
                || StringUtils.isEmpty(apExpertEntity.getExpertLevl()) || StringUtils.isEmpty(apExpertEntity.getExpertSex())){
            return R.error("不能为空");
        }
        if(!StringUtils.isEmpty(apExpertEntity.getExpertIdcard())){
            ApExpertEntity apExpert=expertService.findByIdCard(apExpertEntity.getExpertIdcard());
            if(!StringUtils.isEmpty(apExpert)) {
                return R.error("用户已存在");
            }
        }
        expertService.save(apExpertEntity);
        baseService.logSuccess(3,"添加成功","2");
        return R.ok("添加成功");
    }

}
